var util = require("../../utils/util.js"),
    {
        api
    } = require("../../utils/api.js"),
    app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_no: "",
        detail: null,
        autosize: {
            minHeight: 100
        },
        orderGoodsList: [],
        cart_id: null,
        playType: '1',
        loading: false,
        message: '',
        totalMoney: 0,
        userInfo: null,
        hxOrderSuccess: false,
        goods_type: 1,
        couponDetail: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(option) {
        wx.hideShareMenu()
        // option.order_no='2024120448519753';
        if (option && option.scene) {
            const scene = decodeURIComponent(option.scene);
            if (util.regularConfig.isString(scene) && scene.indexOf(';')) {
                let scenes = scene.split(';');
                if (Array.isArray(scenes) && scenes.length > 1) {
                    let types = scenes[0].split(':');
                    let ids = scenes[1].split(':');
                    let goods_type=Array.isArray(types) && types.length > 1?types[1]:null;
                    let order_id=Array.isArray(ids) && ids.length > 1?ids[1]:null;
                    if(goods_type && order_id){
                        this.setData({goods_type,order_id})
                        console.log('goods_type',this.data.goods_type)
                        console.log('order_id',this.data.order_id)
                        if(goods_type == 1){
                            this.getOrderHxDetail();
                        }else{
                            this.couponQrcodeDetail();
                        }
                    }
                }
            }
        }
    },
    async getOrderHxDetail() {
        const {
            data
        } = await api.getOrderHxDetail(this.data.order_id);
        this.setData({
            orderDetail: data && data.order ? data.order : {},
            orderGoodsList: data && data.order && Array.isArray(data.order.ordergoods) ? data.order.ordergoods : []
        })
    },
    async couponQrcodeDetail() {
        const {
            data
        } = await api.couponQrcodeDetail(this.data.order_id);
        this.setData({
            couponDetail: data && data.coupon ? data.coupon : {}
        })
    },
    hxMethod() {
        if (this.data.goods_type == 1) {
            api.submitOrderHx(this.data.order_id).then(res => {
                this.setData({
                    hxOrderSuccess: true
                })
            })
        } else {
            api.submitOrdercoupon(this.data.order_id).then(res => {
                this.setData({
                    hxOrderSuccess: true
                })
            })
        }

    },

    onChange(e) {
        this.setData({
            playType: e.detail
        })
    },
    onChangeMsg(e) {
        this.setData({
            message: e.detail
        })
    },
    gotoGoodsDetail(e) {
        let id = e.currentTarget.dataset.id;
        console.log('id',id);
        let type = this.data.goods_type == 1 ? (this.data.orderDetail.type == 0 ? 1 : 2) : 2;
        wx.navigateTo({
            url: `/pages/shopping/goodsDetail/goodsDetail?id=${id}&type=${type}`
        })
    },
    //点击一键复制
    copyBtn: function (e) {
        var that = this;
        wx.setClipboardData({
            //准备复制的数据内容
            data: that.data.orderDetail.order_no,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                });
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {

    }
})